package core;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Test;

public class UnitOfMeasurementTest 
{
	private UnitOfMeasurement unit;
	private static double ACCURACY = 0.001;
	
	@After
	public void TearDown()
	{
		unit = null;
	}
	
	@Test
	public void testCount()
	{
		unit = UnitOfMeasurement.count;
		assertEquals("count", unit.toString());
		
		assertTrue(1F == unit.convertFactorTo(UnitOfMeasurement.count));
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.fluid_ounces));
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.gallons));
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.grams));
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.kilograms));
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.liters));
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.ounces));
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.pints));
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.pounds));
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.quarts));
	}
	
	@Test
	public void testFluidOunces()
	{
		unit = UnitOfMeasurement.fluid_ounces;
		assertEquals("fluid ounces", unit.toString());
		
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.count));
		assertTrue(1F == unit.convertFactorTo(UnitOfMeasurement.fluid_ounces));
		assertTrue(Math.abs(128 - unit.convertFactorTo(UnitOfMeasurement.gallons)) < ACCURACY);
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.grams));
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.kilograms));
		assertTrue(Math.abs(33.814 - unit.convertFactorTo(UnitOfMeasurement.liters)) < ACCURACY);
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.ounces));
		assertTrue(Math.abs(16 - unit.convertFactorTo(UnitOfMeasurement.pints)) < ACCURACY);
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.pounds));
		assertTrue(Math.abs(32 - unit.convertFactorTo(UnitOfMeasurement.quarts)) < ACCURACY);
	}
	
	@Test
	public void testGallons()
	{
		unit = UnitOfMeasurement.gallons;
		assertEquals("gallons", unit.toString());
		
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.count));
		assertTrue(Math.abs(0.0078125 - unit.convertFactorTo(UnitOfMeasurement.fluid_ounces)) < ACCURACY);
		assertTrue(1F == unit.convertFactorTo(UnitOfMeasurement.gallons));
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.grams));
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.kilograms));
		assertTrue(Math.abs(0.264172 - unit.convertFactorTo(UnitOfMeasurement.liters)) < ACCURACY);
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.ounces));
		assertTrue(Math.abs(0.125 - unit.convertFactorTo(UnitOfMeasurement.pints)) < ACCURACY);
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.pounds));
		assertTrue(Math.abs(0.25 - unit.convertFactorTo(UnitOfMeasurement.quarts)) < ACCURACY);
	}
	
	@Test
	public void testGrams()
	{
		unit = UnitOfMeasurement.grams;
		assertEquals("grams", unit.toString());
		
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.count));
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.fluid_ounces));
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.gallons));
		assertTrue(1F == unit.convertFactorTo(UnitOfMeasurement.grams));
		assertTrue(Math.abs(1000 - unit.convertFactorTo(UnitOfMeasurement.kilograms)) < ACCURACY);
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.liters));
		assertTrue(Math.abs(28.3495 - unit.convertFactorTo(UnitOfMeasurement.ounces)) < ACCURACY);
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.pints));
		assertTrue(Math.abs(453.592 - unit.convertFactorTo(UnitOfMeasurement.pounds)) < ACCURACY);
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.quarts));
	}
	
	@Test
	public void testKilograms()
	{
		unit = UnitOfMeasurement.kilograms;
		assertEquals("kilograms", unit.toString());
		
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.count));
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.fluid_ounces));
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.gallons));
		assertTrue(Math.abs(0.001 - unit.convertFactorTo(UnitOfMeasurement.grams)) < ACCURACY);
		assertTrue(1F == unit.convertFactorTo(UnitOfMeasurement.kilograms));
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.liters));
		assertTrue(Math.abs(0.0283495 - unit.convertFactorTo(UnitOfMeasurement.ounces)) < ACCURACY);
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.pints));
		assertTrue(Math.abs(0.453592 - unit.convertFactorTo(UnitOfMeasurement.pounds)) < ACCURACY);
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.quarts));
	}
	
	@Test
	public void testLiters()
	{
		unit = UnitOfMeasurement.liters;
		assertEquals("liters", unit.toString());
		
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.count));
		assertTrue(Math.abs(0.0295735 - unit.convertFactorTo(UnitOfMeasurement.fluid_ounces)) < ACCURACY);
		assertTrue(Math.abs(3.78541 - unit.convertFactorTo(UnitOfMeasurement.gallons)) < ACCURACY);
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.grams));
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.kilograms));
		assertTrue(1F == unit.convertFactorTo(UnitOfMeasurement.liters));
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.ounces));
		assertTrue(Math.abs(0.473176 - unit.convertFactorTo(UnitOfMeasurement.pints)) < ACCURACY);
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.pounds));
		assertTrue(Math.abs(0.946353 - unit.convertFactorTo(UnitOfMeasurement.quarts)) < ACCURACY);
	}
	
	@Test
	public void testOunces()
	{
		unit = UnitOfMeasurement.ounces;
		assertEquals("ounces", unit.toString());
		
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.count));
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.fluid_ounces));
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.gallons));
		assertTrue(Math.abs(0.035274 - unit.convertFactorTo(UnitOfMeasurement.grams)) < ACCURACY);
		assertTrue(Math.abs(35.274 - unit.convertFactorTo(UnitOfMeasurement.kilograms)) < ACCURACY);
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.liters));
		assertTrue(1F == unit.convertFactorTo(UnitOfMeasurement.ounces));
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.pints));
		assertTrue(Math.abs(16 - unit.convertFactorTo(UnitOfMeasurement.pounds)) < ACCURACY);
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.quarts));
	}
	
	@Test
	public void testPints()
	{
		unit = UnitOfMeasurement.pints;
		assertEquals("pints", unit.toString());
		
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.count));
		assertTrue(Math.abs(0.0625 - unit.convertFactorTo(UnitOfMeasurement.fluid_ounces)) < ACCURACY);
		assertTrue(Math.abs(8 - unit.convertFactorTo(UnitOfMeasurement.gallons)) < ACCURACY);
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.grams));
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.kilograms));
		assertTrue(Math.abs(2.11338 - unit.convertFactorTo(UnitOfMeasurement.liters)) < ACCURACY);
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.ounces));
		assertTrue(1F == unit.convertFactorTo(UnitOfMeasurement.pints));
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.pounds));
		assertTrue(Math.abs(2 - unit.convertFactorTo(UnitOfMeasurement.quarts)) < ACCURACY);
	}
	
	@Test
	public void testPounds()
	{
		unit = UnitOfMeasurement.pounds;
		assertEquals("pounds", unit.toString());
		
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.count));
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.fluid_ounces));
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.gallons));
		assertTrue(Math.abs(0.00220462 - unit.convertFactorTo(UnitOfMeasurement.grams)) < ACCURACY);
		assertTrue(Math.abs(2.20462 - unit.convertFactorTo(UnitOfMeasurement.kilograms)) < ACCURACY);
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.liters));
		assertTrue(Math.abs(0.0625 - unit.convertFactorTo(UnitOfMeasurement.ounces)) < ACCURACY);
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.pints));
		assertTrue(1F == unit.convertFactorTo(UnitOfMeasurement.pounds));
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.quarts));
	}
	
	@Test
	public void testQuarts()
	{
		unit = UnitOfMeasurement.quarts;
		assertEquals("quarts", unit.toString());
		
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.count));
		assertTrue(Math.abs(0.03125 - unit.convertFactorTo(UnitOfMeasurement.fluid_ounces)) < ACCURACY);
		assertTrue(Math.abs(4 - unit.convertFactorTo(UnitOfMeasurement.gallons)) < ACCURACY);
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.grams));
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.kilograms));
		assertTrue(Math.abs(1.05669 - unit.convertFactorTo(UnitOfMeasurement.liters)) < ACCURACY);
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.ounces));
		assertTrue(Math.abs(0.5 - unit.convertFactorTo(UnitOfMeasurement.pints)) < ACCURACY);
		assertTrue(null == unit.convertFactorTo(UnitOfMeasurement.pounds));
		assertTrue(1F == unit.convertFactorTo(UnitOfMeasurement.quarts));
	}
}
